package com.jit.monitor.presenter.user;


import android.annotation.SuppressLint;

import com.jit.baselib.bean.response.BaseResponse;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.monitor.base.ApiRetrofit;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.view.RestPwdAtView;

import io.reactivex.CompletableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author crazyZhangxl on 2018/11/21.
 * Describe:
 */
public class RestPwdAtPresenter extends BasePresenter<RestPwdAtView> {
    public RestPwdAtPresenter(BaseActivity context) {
        super(context);
    }

    @SuppressLint("CheckResult")
    public void restPwd(String pwd){
        getView().showLoadingDialog();
        ApiRetrofit.getInstance().requestChangePwd(pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseResponse -> {
                    getView().hideLoadingDialog();
                    if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                        UserCache.setUserPwd(pwd);
                        getView().restPwdSuccess();
                    }else {
                        getView().restPwdFailure(baseResponse.getMsg());
                    }
                }, throwable -> {
                    getView().hideLoadingDialog();
                    getView().restPwdFailure(throwable.getLocalizedMessage());
                });
    }
}
